package com.timk.goserver.client.ui.widgets;

import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.UIObject;
import com.timk.goserver.client.ui.screens.MainPanel;

/** */
public class CenteredDialog extends DialogBox {
	protected final UIObject parent;
	
	/**
	 * Creates a centered popup
	 * @param parent the parent to center on
	 */
	public CenteredDialog(UIObject parent) {
		if(parent == null) {
			this.parent = MainPanel.instance;
		} else {
			this.parent = parent;
		}
	}

	protected void onAttach() {
		centerPopup();
		super.onAttach();
	}

	protected void onLoad() {
		centerPopup();
		super.onLoad();
	}
	
	protected void centerPopup() {
		int centerx = parent.getAbsoluteLeft() + parent.getOffsetWidth() / 2;
		int centery = parent.getAbsoluteTop() + parent.getOffsetHeight() / 2;
		setPopupPosition(centerx - getOffsetWidth() / 2,
				centery - getOffsetHeight() / 2);
	}
}
